const Koa = require('koa')
const query = require('./dbConfig/index')
const path = require('path')
const fs = require('fs')
const app = new Koa()
// 黑所有学生信息增加uuid
/*async function handelDb(){
	await query(`ALTER TABLE student ADD COLUMN uuid VARCHAR(36)`)
	await query(`UPDATE student SET uuid = UUID();`)
	await query(`ALTER TABLE student MODIFY uuid VARCHAR(36) UNIQUE`)
	const data = await query(`select uuid,student_name from student`)
	console.log(data)
	for(let i=0;i<data.length;i++){
		const filePath = path.join(__dirname, `./sudentTaskFile/${data[i].uuid}${data[i].student_name}.txt`);
		fs.writeFileSync(filePath, '', 'utf8');
	}
}
handelDb()
*/
/**
 * SELECT table_name FROM information_schema.tables WHERE table_schema = 'qm_studentmanager'; //获取该库下所有的表
 * SELECT CONCAT('ALTER TABLE ', table_name, ' ADD COLUMN school_id INT DEFAULT 1;') FROM information_schema.tables WHERE table_schema = 'qm_studentmanager' AND table_type = 'BASE TABLE'; // 为所有的表增加学校id字段--返回的是一系列增加学校id字段的sql
 */
async function handelDb(){
	const tables=[]
	const t = await query(`SELECT table_name FROM information_schema.tables WHERE table_schema = 'qm_studentmanager'`)
	for(let i=0;i<t.length;i++){
		tables.push(t[i].table_name)
	}
	console.log(tables)
	for(let i=0;i<tables.length;i++){
		if(tables[i]=='user') continue
		await query(` ALTER TABLE ${tables[i]} ADD COLUMN school_id INT DEFAULT 1`)
	}
}
handelDb()

app.listen(3001,()=>console.log("服务已启动,端口号3001"))